﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bzxxxt.bll;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// bg_hzdeads 的摘要说明
    /// </summary>
    public class bg_hzMonthAreaBy : rm_base
    {
        HzBLL hzBLL = new HzBLL();
        public override void getall(HttpContext context)
        {
            string stryear = context.Request.Params["year"];
            string areacode = context.Request.Params["areacode"];
            string type = context.Request.Params["type"];
            if (string.IsNullOrEmpty(areacode))
            {
                areacode = null;
            }
            int? year = null;
            if (stryear == null)
            {
                year = DateTime.Now.Year;
            }
            else if (stryear != "")
            {
                year = int.Parse(stryear);
            }
            string json = hzBLL.hzMonthAreaByUserTypeZhengYear(type,areacode, year);
            context.Response.Write(json);
        }

        public override void other(HttpContext context)
        {
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "export":
                    {
                        string stryear = context.Request.Params["year"];
                        string areacode = context.Request.Params["areacode"];
                        string type = context.Request.Params["type"];
                        if (string.IsNullOrEmpty(areacode))
                        {
                            areacode = null;
                        }
                        int? year = null;
                        if (stryear == null)
                        {
                            year = DateTime.Now.Year;
                        }
                        else if (stryear != "")
                        {
                            year = int.Parse(stryear);
                        }
                        HttpResponse resp = context.Response;
                        resp.Charset = "utf-8";
                        resp.Clear();
                        string filename = stryear + "年度全市死者情况统计表_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                        resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
                        resp.ContentEncoding = System.Text.Encoding.UTF8;

                        resp.ContentType = "application/ms-excel";
                        string style = "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>"
                            + "<style> .table{ font: 13pt Tahoma, Verdana; color: #000000; text-align:center;border-collapse:collapse;border:solid #000 1px; } "
                            + ".table td{text-align:center; border:solid #000 1px;} "
                            + ".table th{text-align:center; font-weight: bold; padding:5px; border:solid #000 1px;} </style>";
                        resp.Write(style);

                        resp.Write("<table class='table' border='1'><tr><th colspan='14'>" + stryear + "年度全市死者情况统计表</th></tr><tr><th>区域名称</th><th>01月</th><th>02月</th><th>03月</th><th>04月</th><th>05月</th><th>06月</th><th>07月</th><th>08月</th><th>09月</th><th>10月</th><th>11月</th><th>12月</th><th>全年</th></tr>");
                        var list = hzBLL.GetHzMonthAreaByUserTypeZhengYearData(type,areacode,year);
                        for (int i = 0; i < list.Count(); i++)
                        {
                            string row = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td><td>{12}</td><td>{13}</td></tr>",
                                 list[i].pname, list[i].m1, list[i].m2, list[i].m3, list[i].m4, list[i].m5, list[i].m6, list[i].m7, list[i].m8, list[i].m9, list[i].m10, list[i].m11, list[i].m12, list[i].mm);
                            resp.Write(row);
                        }
                        resp.Write("</table>");
                        resp.Flush();
                        break;
                    }
            }
        }
    }
}